import pandas as pd
import numpy as np
from sys import exit
from geoDist import distance

def geoEva(boyLng,boyLat,girlLng,girlLat):
    dist = distance(boyLng,boyLat,girlLng,girlLat)
    y = -7e-6 * dist**2 + 100
    return y

def heightEva(boyH, girlH, Hmdiff):
    x = boyH-girlH
    if (x<Hmdiff):
        y = 20 * (x - Hmdiff) + 100
    else:
        y = -12.5 * (x - Hmdiff) + 100
    return y

def ageEva(boyAge,girlAge,Amdiff):
    x = boyAge-girlAge
    if (x>=Amdiff):
        y = -20*(x-Amdiff)+100
    else:
        y = 20*(x-Amdiff)+60
    return y

def expectEva(boyExpect,girlExpect):
    y = keyword(boyExpect) + keyword(girlExpect)
    return y

colnames = ["realName","gender","university","school","major","degree","age","hometown",
            "height","hobby","expect","tel","wechatID","email","photo","suggest","lng","lat"]
all_form = pd.read_excel("include_LngLat.xlsx", names=colnames, dtype="string")
all_form = all_form.astype({"age":"int64", "height":"int64", "lng":"float64", "lat":"float64"})

colList = ["pairID","boyName","boyUniv","boySchool","boyMajor","boyDegree","boyAge","boyHome","boyHeight","boyHobby","boyExpect","boyTel","boyWechat","boyEmail",
           "girlName","girlUniv","girlSchool","girlMajor","girlDegree","girlAge","girlHome","girlHeight","girlHobby","girlExpect","girlTel","girlWechat","girlEmail",'pairValue']
pairForm = pd.DataFrame(data=None,index=None,columns=colList)
pre_pair = []
prePair_num = len(pre_pair)
for i in range(0,prePair_num):
    pairID = i
    boyID = pre_pair[i][0]
    girlID = pre_pair[i][1]

    boyName = all_form.at[boyID, "realName"]
    boyUniv = all_form.at[boyID, "university"]
    boySchool = all_form.at[boyID, "school"]
    boyMajor = all_form.at[boyID, "major"]
    boyDegree = all_form.at[boyID, "degree"]
    boyAge = all_form.at[boyID, "age"]
    boyHome = all_form.at[boyID, "hometown"]
    boyHeight = all_form.at[boyID, "height"]
    boyHobby = all_form.at[boyID, "hobby"]
    boyExpect = all_form.at[boyID, "expect"]
    boyTel = all_form.at[boyID, "tel"]
    boyWechat = all_form.at[boyID, "wechatID"]
    boyEmail = all_form.at[boyID, "email"]

    girlName = all_form.at[girlID, "realName"]
    girlUniv = all_form.at[girlID, "university"]
    girlSchool = all_form.at[girlID, "school"]
    girlMajor = all_form.at[girlID, "major"]
    girlDegree = all_form.at[girlID, "degree"]
    girlAge = all_form.at[girlID, "age"]
    girlHome = all_form.at[girlID, "hometown"]
    girlHeight = all_form.at[girlID, "height"]
    girlHobby = all_form.at[girlID, "hobby"]
    girlExpect = all_form.at[girlID, "expect"]
    girlTel = all_form.at[girlID, "tel"]
    girlWechat = all_form.at[girlID, "wechatID"]
    girlEmail = all_form.at[girlID, "email"]

    maxValue = 300
    pairForm.loc[pairID] = [pairID, boyName, boyUniv, boySchool, boyMajor, boyDegree, boyAge, boyHome, boyHeight, boyHobby,
                       boyExpect, boyTel, boyWechat, boyEmail, girlName, girlUniv, girlSchool, girlMajor, girlDegree,
                       girlAge, girlHome, girlHeight, girlHobby, girlExpect, girlTel, girlWechat, girlEmail, maxValue]
    all_form.drop(index=boyID,inplace=True)
    all_form.drop(index=girlID, inplace=True)

boyForm = all_form[all_form["gender"]=="男"]
boyForm.reset_index(drop=True, inplace=True)
girlForm = all_form[all_form["gender"]=="女"]
girlForm.reset_index(drop=True, inplace=True)
if (len(boyForm)!=len(girlForm)):
    print("inequal error!")
    exit(1)

boyForm.to_excel('男生表.xlsx',index=True)
girlForm.to_excel('女生表.xlsx',index=True)

pairSum = len(boyForm)
initArray = np.zeros((pairSum, pairSum), dtype=float)
grossArray = pd.DataFrame(data=initArray,index=girlForm.index,columns=boyForm.index,dtype=float)
#
Hmdiff = boyForm['height'].mean() - girlForm['height'].mean()
Amdiff = boyForm['age'].mean() - girlForm['age'].mean()
for i in range(0, pairSum):
    for j in range(0, pairSum):
        boyLng = boyForm.at[i,"lng"]
        boyLat = boyForm.at[i,"lat"]
        girlLng = girlForm.at[i,"lng"]
        girlLat = girlForm.at[i,"lat"]
        geoValue = geoEva(boyLng,boyLat,girlLng,girlLat)
        heightValue = heightEva(boyForm.at[i,"height"], girlForm.at[j,"height"], Hmdiff)
        ageValue = ageEva(boyForm.at[i, "age"], girlForm.at[j, "age"], Amdiff)
        expectValue = expectEva(boyForm.at[i,"expect"],girlForm.at[j,"expect"])
        grossArray.iat[j, i] = geoValue + heightValue + ageValue + expectValue
#
tmp_grossArray = grossArray.copy(deep=True)
tmp_grossArray.columns = boyForm['realName']
tmp_grossArray.index = girlForm['realName']
for i in range(0, pairSum):
    (girlID, boyID) = grossArray.stack().idxmax()
    maxValue = grossArray.stack().max()
    girlName = girlForm.at[girlID,"realName"]
    girlUniv = girlForm.at[girlID,"university"]
    girlSchool = girlForm.at[girlID,"school"]
    girlMajor = girlForm.at[girlID,"major"]
    girlDegree = girlForm.at[girlID,"degree"]
    girlAge = girlForm.at[girlID, "age"]
    girlHome = girlForm.at[girlID, "hometown"]
    girlHeight = girlForm.at[girlID, "height"]
    girlHobby = girlForm.at[girlID, "hobby"]
    girlExpect = girlForm.at[girlID, "expect"]
    girlTel = girlForm.at[girlID, "tel"]
    girlWechat = girlForm.at[girlID, "wechatID"]
    girlEmail = girlForm.at[girlID, "email"]

    boyName = boyForm.at[boyID, "realName"]
    boyUniv = boyForm.at[boyID, "university"]
    boySchool = boyForm.at[boyID, "school"]
    boyMajor = boyForm.at[boyID, "major"]
    boyDegree = boyForm.at[boyID, "degree"]
    boyAge = boyForm.at[boyID, "age"]
    boyHome = boyForm.at[boyID, "hometown"]
    boyHeight = boyForm.at[boyID, "height"]
    boyHobby = boyForm.at[boyID, "hobby"]
    boyExpect = boyForm.at[boyID, "expect"]
    boyTel = boyForm.at[boyID, "tel"]
    boyWechat = boyForm.at[boyID, "wechatID"]
    boyEmail = boyForm.at[boyID, "email"]

    pairID = i + prePair_num
    pairForm.loc[pairID] = [pairID, boyName, boyUniv, boySchool, boyMajor, boyDegree, boyAge, boyHome, boyHeight, boyHobby, boyExpect, boyTel, boyWechat, boyEmail,
                       girlName, girlUniv, girlSchool, girlMajor, girlDegree, girlAge, girlHome, girlHeight, girlHobby, girlExpect, girlTel, girlWechat, girlEmail, maxValue]
    grossArray.drop(index=girlID,inplace=True)
    grossArray.drop(columns=boyID,inplace=True)
#
pairForm.to_excel('配对表.xlsx',index=False)

print("finished!")
